BIN_FILE = ucosii4pxa255
UCOS_DIR=./ucos
LWIP_DIR = ./lwip
LIBHTTPD_DIR = ./libhttpd

#CROSS_COMPILE ?= arm-linux-
#CROSS_COMPILE ?= /usr/local/arm/3.3.2/bin/arm-linux-
CROSS_COMPILE ?= /usr/local/arm/3.4.1/bin/arm-linux-
#CROSS_COMPILE ?= arm-elf-
CC = $(CROSS_COMPILE)gcc
LD = $(CROSS_COMPILE)ld
OBJDUMP = $(CROSS_COMPILE)objdump
OBJCOPY = $(CROSS_COMPILE)objcopy

#INCLUDE = -I./include -I./eth -I$(UCOS_DIR) -I$(LIBHTTPD_DIR)
INCLUDE = -I. -I./include -I./eth -I$(UCOS_DIR) -I$(LWIP_DIR)/include -I$(LWIP_DIR)/arch/ucosII/include -I$(LWIP_DIR)/include/ipv4 -I$(LIBHTTPD_DIR)
CFLAGS = -O2 -nostdinc -mcpu=strongarm110 -mapcs-32 -fomit-frame-pointer -fPIC $(INCLUDE)  -Wall $(CFLAGSII)
#CFLAGS = -O2 -nostdinc -mcpu=strongarm110 -mapcs-32 -fPIC $(INCLUDE)  -Wall $(CFLAGSII) -g
#CFLAGS = -O2 -mlittle-endian -march=armv4 -mtune=arm7tdmi -Wall -g -Wstrict-prototypes -Wno-trigraphs -pipe -mshort-load-bytes -mapcs-32
LIB = -lc -lgcc

OCFLAGS = -O binary -R .note -R .comment -S
LDFLAGS = -static -nostdlib
ELF32_LDFLAGS = -Tld-xscale
COMPILE = $(CC) $(CFLAGS)

CFILES = $(wildcard *.c)
SFILES = $(wildcard *.S)

UCOS_SFILES = $(wildcard $(UCOS_DIR)/*.S)
UCOS_CFILES = $(wildcard $(UCOS_DIR)/*.c) 
UCOS_FILES = $(UCOS_SFILES) $(UCOS_CFILES)
UCOS_OBJS = $(notdir $(UCOS_SFILES:.S=.o)) $(notdir $(UCOS_CFILES:.c=.o)) 

LWIP_API_FILES = $(wildcard $(LWIP_DIR)/api/*.c)
LWIP_CORE_FILES = $(wildcard $(LWIP_DIR)/core/*.c) \
				  $(wildcard $(LWIP_DIR)/core/ipv4/*.c)
LWIP_NETIF_FILES = $(wildcard $(LWIP_DIR)/netif/*.c)
LWIP_ARCH_FILES = $(wildcard $(LWIP_DIR)/arch/ucosII/*.c) \
				  $(wildcard $(LWIP_DIR)/arch/ucosII/netif/*.c)

LWIP_FILES = $(LWIP_API_FILES) $(LWIP_CORE_FILES) $(LWIP_NETIF_FILES) $(LWIP_ARCH_FILES)
LWIP_OBJS = $(notdir $(LWIP_API_FILES:.c=.o)) \
	        $(notdir $(LWIP_CORE_FILES:.c=.o)) \
			$(notdir $(LWIP_NETIF_FILES:.c=.o)) \
			$(notdir $(LWIP_ARCH_FILES:.c=.o))

LIBHTTPD_FILES = $(wildcard $(LIBHTTPD_DIR)/*.c)
LIBHTTPD_OBJS = $(notdir $(LIBHTTPD_FILES:.c=.o))

#OBJ_FILES = $(SFILES:%.S=%.o) $(CFILES:%.c=%.o)
OBJ_FILES = $(SFILES) $(CFILES) $(UCOS_FILES) $(LWIP_FILES) $(LIBHTTPD_FILES)
OBJ = $(SFILES:.S=.o) $(CFILES:.c=.o) $(UCOS_OBJS) $(LWIP_OBJS) $(LIBHTTPD_OBJS)

#OBJ_FILES = start.o main.o command.o serial.o time.o flash.o util.o uucodec.o scc.o net.o bootp.o tftp.o


all : $(BIN_FILE)
	@echo "done"

ifeq (.depend, $(wildcard .depend))
include .depend
endif

%.o :
	@echo "complie file:(" $< ")" #$@
	$(CC) -c $(CFLAGS) $(INCLUDE) $(<:.o=.c)
	@echo

dep : $(OBJ_FILES)
	$(CC) $(CFLAGS) $(INCLUDE) -MM $^ > .depend || rm -f .depend

$(BIN_FILE) : $(OBJ)
	$(CC) $(LDFLAGS) -o $(BIN_FILE)-elf32 $(ELF32_LDFLAGS) $(OBJ) $(LIB)
	$(OBJCOPY) $(OCFLAGS) $(BIN_FILE)-elf32 $(BIN_FILE)
	cp ucosii4pxa255 /tftpboot/ucos

test : all
	skyeye -e $(BIN_FILE)-elf32

test_out : all
	skyeye -e $(BIN_FILE)-elf32 >out 2>&1

debug : all
	skyeye -e $(BIN_FILE)-elf32 -d

btest : all
	skyeye 

btest_out : all
	skyeye >out 2>&1

bdebug : all
	skyeye -d

clean :
	rm -rf *.o
	rm -rf $(BIN_FILE) $(BIN_FILE)-elf32 .depend

do :
	make clean
	make dep
	make
